package com.example.myapplication.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private ListView listView;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);


        return root;
    }

    class MyAdapter extends BaseAdapter{
        FindHouseBean findHouseBean ;

        public MyAdapter(FindHouseBean findHouseBean) {
            this.findHouseBean = findHouseBean;
        }

        @Override
        public int getCount() {
            return findHouseBean.getRows().size();
        }

        @Override
        public Object getItem(int position) {
            return findHouseBean.getRows().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getContext(), R.layout.list_item_layout, null);
            ImageView ivFangzi = (ImageView) view.findViewById(R.id.iv_fangzi);
            TextView tvName = (TextView) view.findViewById(R.id.tv_name);
            FindHouseBean.RowsBean fangzi = findHouseBean.getRows().get(position);
            tvName.setText( fangzi.getSourceName());
            Glide.with(getContext())
                    .load("http://124.93.196.45:10001"+fangzi.getPic()).centerCrop().into(ivFangzi);

            return view;
        }
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Banner banner = (Banner) view.findViewById(R.id.banner);
        // 观察数据的变化
        homeViewModel.getFindHouseBeanMutableLiveData().observe(getViewLifecycleOwner(), new Observer<FindHouseBean>() {
            @Override
            public void onChanged(FindHouseBean findHouseBean) {
                //TODO 更新界面，创建适配器。。适配器一个通过数据和项布局确定每一项显示外观的类
                MyAdapter adapter = new MyAdapter(findHouseBean);
                listView.setAdapter(adapter);
            }
        });

        //放图片地址的集合
        List list_path = new ArrayList<>();
        //放标题的集合
        List list_title = new ArrayList<>();

        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");
        list_title.add("好好学习");
        list_title.add("天天向上");
        list_title.add("热爱劳动");
        list_title.add("不搞对象");

        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        banner.setImages(list_path);
        //设置轮播图的标题集合
        banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER).start();

        listView = view.findViewById(R.id.listview);
        TabLayout tab = view.findViewById(R.id.tab_fangzi);
        tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition() ){
                    case 0:
                        homeViewModel.getData(getContext(),"二手");
                        break;
                    case 1:
                        homeViewModel.getData(getContext(),"租房");
                        break;
                    case 2:
                        homeViewModel.getData(getContext(),"楼盘");
                        break;
                    case 3:
                        homeViewModel.getData(getContext(),"中介");
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }
    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((String) path).into(imageView);
        }
    }
}